import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.float_format', lambda x: '%.7f' % x)
np.set_printoptions(suppress=True, precision=4, threshold=np.inf)
report_path = r"C:\Users\huajia\Desktop\rqalpha4\rqalpha\plot_result\my_first_strategy_file4"
trades = []
portfolios = []
for file in os.listdir(report_path):
    trade_file = os.path.join(report_path, file, 'trades.csv')
    trade_df = pd.read_csv(trade_file, encoding='gbk')
    if not trade_df.empty and trade_df.shape[0] >= 2:
        print(trade_df['order_book_id'].values[0])
        symbol = trade_df['order_book_id'].values[0]
        portfolio_csv = os.path.join(report_path, file, 'portfolio.csv')
        portfolio_df = pd.read_csv(portfolio_csv, encoding='gbk')
        portfolio_df[['strategy_total_return', 'strategy_annual_return']] = \
            portfolio_df[['strategy_total_return', 'strategy_annual_return']] / 1000
        portfolio_df = portfolio_df[['date', 'strategy_total_return', 'strategy_annual_return']]
        portfolio_df['symbol'] = symbol
        trades.append(trade_df)
        portfolios.append(portfolio_df)
if len(trades) > 0:
    trade = pd.concat(trades, sort=False, ignore_index=True)
    portfolio = pd.concat(portfolios, sort=False, ignore_index=True)
    print(len(trade['order_book_id'].unique().tolist()))
    # trade.to_csv('trades.csv')
    # print(trade)
    portfolio = portfolio.drop_duplicates()
    # portfolio.to_csv('portfolio.csv')
    portfolio = portfolio.groupby(['date'], as_index=False).agg({'strategy_total_return': 'mean'})
    print(portfolio)
    fig, ax = plt.subplots()
    y_major_locator = MultipleLocator(0.00001)
    ax.yaxis.set_major_locator(y_major_locator)
    # for n, group in portfolio.groupby('date'):
    #     group.plot(x='date', y='strategy_total_return', ax=ax, label=n)
    plt.plot(portfolio['date'], portfolio['strategy_total_return'])
    plt.show()





